<template>
  <views class="recharge_management">
    <viewTitle :title="'账户详情'" :border="false"></viewTitle>
    <!-- 搜索栏  企业端-代理商端-服务商端 -->
    <div class="search flex flexC">
      <!-- 所属服务商-银行流水号-客户编号-记录类型 -->
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="search_input flex flexR center">
          <div class="bank_statement flex flexR">
            <el-form-item label="银行流水号:">
              <el-input v-model="form.bank_payment_sn" placeholder="银行流水号" :clearable="true"></el-input>
            </el-form-item>
          </div>
          <div class="customer_number flex flexR center">
            <el-form-item label="公司编码:">
              <el-input v-model="form.enterprise_sn" placeholder="公司编码" :clearable="true"></el-input>
            </el-form-item>
          </div>
          <div class="customer_number flex flexR center">
            <el-form-item label="公司名称:">
              <el-input v-model="form.enterprise_name" placeholder="公司名称" :clearable="true"></el-input>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <!-- 充值时间  按钮 (查询、重置、导出列表、录单)-->
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="pay_time flex flexR center">
          <div class="customer_number flex flexR center">
            <el-form-item label="代理商编号:">
              <el-input v-model="form.agent_sn" placeholder="代理商编号" :clearable="true"></el-input>
            </el-form-item>
          </div>
          <div class="record_type flex flexR center">
            <el-form-item label="所属代理商:">
              <el-input
                      v-model="form.agent_name"
                      placeholder="所属代理商"
                      :clearable="true"
              ></el-input>
              <!--<el-select v-model="form.agent_id" clearable placeholder="请选择">-->
              <!--<el-option-->
              <!--v-for="item in agent_list"-->
              <!--:key="item.agent_id"-->
              <!--:label="item.agent_name"-->
              <!--:value="item.agent_id"-->
              <!--&gt;</el-option>-->
              <!--</el-select>-->
            </el-form-item>
          </div>
          <div class="record_type flex flexR center">
            <el-form-item label="记录类型:">
              <el-select v-model="form.record_type" clearable placeholder="请选择">
                <el-option
                  v-for="item in record_type_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="pay_state flex flexR center">
            <el-form-item label="充值状态:">
              <el-select v-model="form.charge_status" clearable placeholder="请选择">
                <el-option
                  v-for="item in charge_status_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="pay_time flex flexR center">
          <!-- 入账时间 -->
          <div class="pay_time flex flexR center">
            <el-form-item label="入账时间:">
              <el-date-picker
                v-model="pay_time"
                type="datetimerange"
                align="right"
                :default-time="['0:00:00','23:59:59']"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                @change="changeTime"
              ></el-date-picker>
            </el-form-item>
          </div>
          <!-- 按钮（查询、重置、导出列表、录单）  录单属于服务商端 -->
          <el-form-item>
            <div class="flex flexR pay_btn_list">
              <el-button type="primary" @click="getData" icon="el-icon-search">查询</el-button>
              <el-button icon="el-icon-refresh" @click="reset()">重置</el-button>
              <el-button type="danger" @click="exportExcel" icon="el-icon-download" ref="ddd">导出列表</el-button>
              <el-button
                type="success"
                v-if="this.$store.state.terrace==3"
                @click="our_catalogue"
              >录单</el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="search_input flex flexR center">
      <div :style="{marginLeft:'15px'}" class="show-title-money flex flexR">
        <div class="flex flexR">
          <div>充值笔数</div>
          <div class="price">{{pagination.total_count}}</div>
          <div>笔</div>
        </div>
        <div class="flex flexR">
          <div>充值金额(元)</div>
          <div class="price">￥{{pagination.summy ? $splitK(pagination.summy) : ''}}</div>
        </div>
      </div>
    </div>
    <!-- 展示列表 -->
    <div class="table_list">
      <el-table
        border
        :data="tableData"
        style="width: 100%;max-width:none;"
        :header-cell-style="getRowClass"
        :select-on-indeterminate="true"
        :fit="true"
        :lazy="true"
        @row-click="clickRow"
        max-height="450"
      >
        <el-table-column  type="index" label="序号" ></el-table-column>
        <el-table-column prop="enterprise_order_sn" label="业务订单号"></el-table-column>
        <el-table-column prop="record_type" label="记录类型" ></el-table-column>
        <el-table-column  prop="enterprise_sn" label="公司编码"></el-table-column>
        <el-table-column prop="enterprise_name" label="公司名称" ></el-table-column>
        <el-table-column prop="agent_sn" label="代理商编码" width="100"></el-table-column>
        <el-table-column prop="agent_name" label="所属代理商" width="100"></el-table-column>
        <el-table-column prop="user_name" label="所属客户经理" width="110" ></el-table-column>
        <el-table-column prop="facilitator_name" label="服务商名称" width="100"></el-table-column>
        <el-table-column :formatter="(row) => {return $splitK(row.real_money)}" width="110" label="充值金额"></el-table-column>
        <el-table-column prop="created_time" :formatter="timeFormat" label="操作时间"></el-table-column>
        <el-table-column prop="bank_payment_sn" label="银行流水号" width="110"></el-table-column>
        <el-table-column prop="bank_name" label="付款银行" ></el-table-column>
        <el-table-column prop="charge_status_name" label="充值状态" ></el-table-column>
        <el-table-column prop="process_rate" label="充值费率" ></el-table-column>
        <el-table-column :formatter="(row) => {return $splitK(row.plat_money)}" label="入账金额" ></el-table-column>
        <el-table-column
          prop="pay_time"
          :formatter="timeFormat"
          label="入账时间"
          v-if="isCheck || isRefuse"
        ></el-table-column>
        <el-table-column label="操作" width="170"  v-if="show_edit && getTerrace == 3">
          <div class="table_operation flex flexR" slot-scope="scope" v-show="getTerrace == 3">
            <!--  -->
            <el-button
              type="primary"
              v-if="scope.row.charge_status==1 && isCheck"
              @click="charge_confirm(scope.row.enterprise_order_ext_id)"
            >审核</el-button>
            <el-button
              type="primary"
              v-if="scope.row.charge_status==1 && isRefuse"
              @click="charge_refuse(scope.row.enterprise_order_ext_id)"
            >拒绝</el-button>
          </div>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex flexR paging">
      <el-pagination
        style="background:#fff; padding:10px 0"
        background
        layout="total, sizes,prev, pager, next, jumper"
        @size-change="(page_size) => {this.pagination.page_size = page_size;this.getData()}"
        @current-change="(page_size) => {this.pagination.page_start = page_size;this.getData()}"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagination.page_size"
        :total="pagination.total_count"
        :current-page="pagination.current_page"
        :page-count="pagination.page_count"
      ></el-pagination>
    </div>
  </views>
</template>



<script>
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      //  三段综合  企业端  代理商端  服务商端
      download_url: "",
      form: {
        belong_facilitator: "", //  所属服务商
        enterprise_sn: "", //  企业编码
        enterprise_name: "", //  客户名称
        enterprise_order_sn: "", //  记录类型
        bank_payment_sn: "", //  充值状态
        record_type: "", //  客户编号
        agent_id: 0, //  客户编号
        charge_status: "", //  充值时间
        charge_time_start: "", //  充值时间
        charge_time_end: "" //  充值时间
      },
      pay_time: "",
      money_sum: 0,
      tableData: [],
      // 1：人工录入、2：系统自动
      record_type_list: [
        { value: "", label: "全部" },
        { value: "1", label: "人工录入" },
        { value: "2", label: "系统自动" }
      ], //  记录类型列表
      //（1：待确认、2：待记账、3：已记账、4：无效充值
      charge_status_list: [
        { value: "", label: "全部" },
        { value: "1", label: "待确认" },
        // { value: "2", label: "待记账" },
        { value: "3", label: "已记账" },
        { value: "4", label: "无效充值" }
      ], //  充值状态列表
      pagination: {
        //分页数据
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      },
      agent_list: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      show_edit: true,
      autofocus1: true,
      autofocus2: false
    };
  },
  mounted() {
    this.getAgentList();
    this.getData();
  },
  computed: {
    getTerrace() {
      return this.$store.state.terrace;
    },
    getComfrimCharge() {
      //查看改服务商管理员是否有审核权限
      return this.$store.state.buttonPermission.comfrimCharge
        ? this.$store.state.buttonPermission.comfrimCharge
        : "";
    },
    ...mapGetters(["getButtonPermission"]),
    isCheck() {
      return this.getButtonPermission[this.$route.path] &&
        this.getButtonPermission[this.$route.path].check
        ? 1
        : 0;
    },
    isRefuse() {
      return this.getButtonPermission[this.$route.path] &&
        this.getButtonPermission[this.$route.path].refuse
        ? 1
        : 0;
    }
  },
  methods: {
    //    修改第一行样式
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #FAFAFA ";
      } else {
        return "";
      }
    },
    //     点击单行
    clickRow(row, column, event) {
      console.log(row, column, event);
    },
    //    修改是否换行
    changeStyleRow(width) {
      let _width = width - 290 - 350 - 300 - 50 - 25;
      if (_width > 300) {
        this.marginLeft = 25;
      } else {
        this.marginLeft = 0;
      }
    },
    //  搜索/查询
    getData() {
      this.form.pagination = this.pagination;
      console.log("查询数据", this.form);
      if (this.$store.state.terrace == 1) {
        this.getEnterpriseData();
      }
      if (this.$store.state.terrace == 2) {
        this.getAgentData();
      }
      if (this.$store.state.terrace == 3) {
        this.getFacilitatorData();
      }
    },
    //  重置
    reset() {
      this.form = {
        belong_facilitator: "", //  所属服务商
        enterprise_sn: "", //  企业编码
        enterprise_name: "",
        enterprise_order_sn: "", //  记录类型
        bank_payment_sn: "", //  充值状态
        record_type: "", //  客户编号
        agent_id: 0, //
        agent_name:'', //所属代理商
        charge_status: "", //  充值时间
        charge_time_start: "", //  充值时间
        charge_time_end: "", //  充值时间
        agent_sn:"",
      };
      this.pay_time = [];
      this.getData();
    },
    //   录单
    our_catalogue() {
      this.$toRoutePath({
        path: "/facilitator_account_query/recharge_catalogue",
        edit: true,
        query: {}
      });
    },
    //获取企业端数据
    getEnterpriseData() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.eGetChargeList,
        this.form
      );
      if (code != 200 && code != 501) {
        this.$message.error(msg);
        return;
      }

      this.tableData = data.list || [];
      this.pagination = data.pagination;
      this.download_url = data.download_url;
      this.money_sum = data.money_sum;
      this.btn_show(data.list);
    },
    //获取服务商端数据
    getAgentData() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.arecharge,
        this.form
      );
      if (code != 200 && code != 501) {
        this.$message.error(msg);
        this.enterprise.tableData = [];
        return;
      }
      // this.data = data;
      this.tableData = data.list || [];
      this.tableData = [];
      this.pagination = data.pagination;
      this.download_url = data.download_url;
      this.btn_show(data.list);
    },
    //获取代理商端数据
    getFacilitatorData() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.fchargeList,
        this.form
      );
      if (code != 200 && code != 501) {
        this.tableData = [];
        this.$message.error(msg);
        return;
      }
      // this.data = data;
      this.tableData = data.list || [];
      this.pagination = data.pagination;
      this.download_url = data.download_url;
      this.btn_show(data.list);
    },
    timeFormat(row, col, idx) {
      // console.log("查询数据-col", idx);
      return idx ? this.$moment(idx * 1000).format("YYYY-MM-DD HH:MM:SS") : "";
    },
    exportExcel() {
      this.$import_excal(this.download_url || "");
    },
    changeTime() {
      if (this.pay_time) {
        this.form.charge_time_start = this.pay_time[0]/1000
        this.form.charge_time_end = this.pay_time[1]/1000
      } else {
        this.form.charge_time_start = "";
        this.form.charge_time_end = "";
      }
    },
    getAgentList() {
      if (this.$store.state.terrace == 3) {
        let { code, msg, data } = this.$api.sendSync(
          this.$mapapi.f_getAgentList,
          {
            pagination: {
              //分页数据
              page_start: 1, //搜索开始页码
              page_size: 100000 //每页数量
            }
          }
        );
        this.agent_list = [{ agent_id: 0, agent_name: "全部" }];
        if (code != 200 && code != 501) {
          console.log(msg);
          return;
        } else {
          this.agent_list = [...this.agent_list, ...data.list];
        }
      } else {
        this.agent_list = [];
      }
    },
    charge_confirm(enterprise_order_ext_id) {
      this.$confirm("你确定审核么, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let { code, msg } = this.$api.sendSync(this.$mapapi.fcomfrimCharge, {
          enterprise_order_ext_id: enterprise_order_ext_id
        });
        if (code != 200) {
          this.$message.error(msg);
          return;
        }
        this.getData();
        this.$message({
          type: "success",
          message: "审核成功!"
        });
      });
    },
    charge_refuse(enterprise_order_ext_id) {
      this.$confirm("你确定拒绝么, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let { code, msg } = this.$api.sendSync(this.$mapapi.frefuseCharge, {
          enterprise_order_ext_id: enterprise_order_ext_id
        });
        if (code != 200) {
          this.$message.error(msg);
          return;
        }
        this.getData();
        this.$message({
          type: "success",
          message: "拒绝成功!"
        });
      });
    },
    // 按钮展示
    btn_show(list) {
      let newList = [];
      if (Array.isArray(list)) {
        list.forEach((v, k) => {
          if (v.charge_status == 1) {
            newList.push(11);
          }
        });
        if (newList.length > 0) {
          this.show_edit = true;
        } else {
          this.show_edit = false;
        }
      }
    }
  },
  created() {}
};
</script>


<style lang="less" :scope="true"  src="@/style/account/recharge_management.less" >
</style>